package com.salas.action;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.log.Log;
import org.jboss.seam.security.Identity;

import com.salas.entity.Customer;

@Name("authenticator")
public class Authenticator {
	@Logger
	Log log;

	@In
	private EntityManager entityManager;

	@In
	private Identity identity;
	

    @Out(required=false, scope=ScopeType.SESSION) 
    Customer currentCustomer;	
	

	public boolean authenticate() {
		log.info("authenticating #0",identity.getUsername());
		try {
			currentCustomer = (Customer) entityManager.createQuery(
					"from Customer where customerName = :customerName").setParameter(
					"customerName",identity.getUsername()).getSingleResult();

			if (currentCustomer.getPassword().equals(identity.getPassword())) {
				//System.out.print(customer.getCustomerName());
				//System.out.print(identity.getUsername());
				//System.out.print(customer.getPassword());
				//System.out.print(identity.getPassword());
				return true;
			}

			return false;
		} catch (NoResultException ex) {
			return false;
		}

	}
}
